<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" class="handle-del mr10" @click="openAddDialog" size="small">新增员工</el-button><br/><br/>      
        <el-form label-width="70px">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="员工编号"><el-input placeholder="请输入编号" v-model="query.userNum"></el-input></el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="姓名"><el-input placeholder="请输入姓名" v-model="query.userName"></el-input></el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="用户名"><el-input placeholder="请输入用户名" v-model="query.name"></el-input></el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="手机号"><el-input placeholder="请输入手机号" v-model="query.userPhone"></el-input></el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="身份证号"><el-input placeholder="请输入身份证号" v-model="query.userIdCard"></el-input></el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="在职状态">
                <el-radio-group v-model="query.userStatus" size="small">
                  <el-radio-button label="1">在职</el-radio-button>
                  <el-radio-button label="0">离职</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24" align="center">
              <el-button type="primary" @click="clearSearch" size="small">清空</el-button>
              <el-button type="primary" @click="handleSearch" size="small">搜索</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header" @selection-change="handleSelectionChange">
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">{{ (query.currentPage - 1)*query.pageSize + (scope.$index + 1) }}</template>
        </el-table-column>
        <el-table-column prop="userNum" label="员工编号" align="center"></el-table-column>
        <el-table-column prop="userName" label="姓名" align="center"></el-table-column>
        <el-table-column prop="name" label="用户名" align="center"></el-table-column>
        <el-table-column prop="shopName" label="所属分店" align="center"></el-table-column>
        <el-table-column label="当前状态" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.userStatus===1?'success':'danger'">{{ scope.row.userStatus===1?'在职':'离职' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="roleName" label="角色" align="center"></el-table-column>
        <el-table-column prop="userIdCard" label="身份证号" align="center"></el-table-column>
        <el-table-column prop="userPhone" label="联系电话" align="center"></el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.currentPage"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <el-dialog :title="dialogTitle" :visible.sync="editVisible" width="70%" :close-on-click-modal="false">
      <el-form ref="form" border :model="addForm" label-width="70px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="员工编号">
              <el-input placeholder="请输入编号" v-model="addForm.userNum"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户姓名">
              <el-input placeholder="请输入姓名" v-model="addForm.userName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号">
              <el-input placeholder="请输入手机号" v-model="addForm.userPhone"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="身份证号">
              <el-input placeholder="请输入身份证号" v-model="addForm.userIdCard"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="登录账户">
              <el-input placeholder="请输入登录账户" v-model="addForm.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="密码">
              <el-input placeholder="请输入密码" v-model="addForm.password" show-password></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="所属分店">
              <el-input v-model="addForm.shopName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属分组">
              <el-input v-model="addForm.userGroup"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="在职状态">
              <el-radio-group v-model="addForm.userStatus" size="small">
                <el-radio-button label="1">在职</el-radio-button>
                <el-radio-button label="0">离职</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="权限角色">
              <el-checkbox-group v-model="addForm.roleIds">
                <el-checkbox v-for="role in allRoleName" :label="role.role_id" :key="role.role_id">{{ role.role_name }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="addUpdUserInfo" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
let queryParam = { userNum:'',userName:'',name:'',userPhone:'',userIdCard:'',userStatus:"1",currentPage:1,pageSize:8 };
let addFormParam = { userNum:'',userName:'',userPhone:'',userIdCard:'',name:'',shopName:'',userGroup:'',userStatus:'1',roleIds:[] };

export default {
  data() {
    return {
      query: {...queryParam},
      tableData: [],
      multipleSelection: [],
      delList: [],
      dialogTitle: '',
      editVisible: false,
      pageTotal: 0,
      addForm: {...addFormParam},
      allRoleName: [],
      idx: -1,
      id: -1,
      isAdd: false
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.$axios.get(this.$api.customer.findAll, this.query)
      .then(res => {
        console.log("所有客户信息__"+JSON.stringify(res));
        if(res.data.code=="1" && res.data.success==true){
          /* this.tableData = res.data.data.list;
          this.pageTotal = res.data.data.total; */
        }
      })
      .catch(err => { 
        console.log(err);
        this.tableData = [];
        this.pageTotal = 0;
      });
    },
    //新增或修改用户信息
    addUpdUserInfo() {
      if(this.isAdd) {
        console.log("新增信息为："+JSON.stringify(this.addForm));
        /* this.$axios.post(this.$api.user.addUser, this.addForm)
        .then(res => {
          if(res.data.code=="1" && res.data.success==true){
            this.$message.success("新增成功");
          }
          this.getData();
          this.editVisible = false;
        })
        .catch(err => { console.log(err); }); */
      } else {
        console.log("修改的数据___"+JSON.stringify(this.addForm));
        /* this.$axios.post(this.$api.user.updateUser, this.addForm)
        .then(res => {
          console.log("修改的结果为："+JSON.stringify(res));
          if(res.data.code=="1" && res.data.success==true){
            this.$message.success("修改成功");
          }
          this.getData();
          this.editVisible = false;
        })
        .catch(err => { console.log(err); }); */
        this.$set(this.tableData, this.idx, this.form);
      }
    },
    //清空搜索条件
    clearSearch() {
      this.query = {...queryParam};
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "currentPage", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
      .then(() => {
        /* this.$axios.get(this.$api.user.deleteByPhone+"?phone="+row.userPhone)
        .then(res => {
          if(res.data.code=="1" && res.data.success==true){
            this.$message.success("删除成功");
            this.getData();
          } else if(res.data.code=="0" && res.data.success==false) {
            this.$message.warning(res.data.msg);
          }
        })
        .catch(err => { console.log(err); }); */
      })
      .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //打开新增员工的弹框
    openAddDialog() {
      this.addForm = {...addFormParam};
      this.dialogTitle = "新增员工信息";
      this.isAdd = true;
      this.editVisible = true;
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.addForm.roleIds = [];
      this.$axios.get(this.$api.user.findById+"?id="+row.id)
      .then(res => {
        //console.log("获取的数据为__"+JSON.stringify(res));
        this.addForm = res.data.data;
        this.addForm.id = row.id;
      })
      .catch(err => { console.log(err); });
      this.dialogTitle = "编辑员工信息";
      this.isAdd = false;
      this.editVisible = true;
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "currentPage", val);
      this.getData();
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>